# 交易商
# build in python3.5
# write by hashaki 创建时间：20181019   最后一次修改 2018/11/24
from collections import OrderedDict

class BacktestBroker:
    def __init__(self,backTestingEngine=None):
        self.backTestingEngine=backTestingEngine
        self.capital=None                 # 有多少钱
        self.order=OrderedDict()

    def accountInfo(self):
        '''账户信息'''
        return self.capital

    def buy(self,target_symbol,type_,price,volume,strategy_name):
        '''买买买，计算还是在主引擎'''
        self.backTestingEngine.sendOrder(target_symbol,type_,price,volume,strategy_name)

    def sell(self,target_symbol,type_,price,volume,strategy_name):
        '''卖卖卖，计算还是在主引擎'''
        self.backTestingEngine.sendOrder(target_symbol,type_,price,volume,strategy_name)

    def cancel(self,target_symbol,OrderID=None):
        '''撤销订单,若orderID为空，说明是全部撤单，若有ID，则扯该ID的单'''
        self.backTestingEngine.cancelOrder(target_symbol,OrderID=OrderID)

    def short(self,target_symbol,type_,price,volume,strategy_name):
        """卖开"""
        self.backTestingEngine.sendOrder(target_symbol,type_,price,volume,strategy_name)
    
    def cover(self,target_symbol,type_,price,volume,strategy_name):
        """买平"""
        self.backTestingEngine.sendOrder(target_symbol,type_,price,volume,strategy_name)

    def getOrderInfo(self):
        pass

    def getOrderHistory(self):
        if not self.order:
            print("还没有下单记录")
            return
        else:
            for i,k in self.order:
                print("下单历史：",k)

    def getFundHistory(self):
        pass

    def getCommissionHistory(self):
        pass

    def setCash(self,cash):
        '''设置初始现金'''
        if isinstance(cash,int):
            self.capital=cash
        else:
            raise ValueError("现金初始值必须为int整数值")

    def getCash(self):
        print('还剩现金：',self.capital)
        return self.capital

    def getValue(self):
        pass

    def getPosition(self):
        pass

    def submit(self):
        raise NotImplementedError

    def sendOrder(self):
        '''把交易信息传到交易所，回测中则传到回测引擎'''
        pass